﻿using Halcon二次开发.Controller.CamerForm交互;
using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Model;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Model.图像绑定流程图显示;
using Halcon二次开发.Model.自定义控件;
using MvCameraControl;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.View.相机
{
    public partial class CamerForm : Form
    {
        /// <summary>
        ///实时图像窗体 
        /// </summary>
        RealTimeCamerForm realTimeCamerForm;
        public CamerFormController controller;
        public CamerForm()
        {
            InitializeComponent();
            //实例化一次动作类
            realTimeCamerForm = new RealTimeCamerForm();//生成一个实时现实图像的窗口
            controller = new CamerFormController(StaicResouce.HwindowFlowNodeUrse, realTimeCamerForm,this);
        }
        public void SetJsonPaths(string path, string addres)
        {
            string paths = path + "\\" + "界面数据\\" + addres + "\\" + addres + "数据.json";
            //调用存储方法
            new LoadData().LoadDatas(paths, this);
        }
        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            controller.OpenDirectory(textBox1, flowLayoutPanel1,label1);
        }

        private void CamerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="sender"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            controller.OpenFile(textBox1,flowLayoutPanel1,label1);
        }
        /// <summary>
        /// 刷新按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       
        private void button5_Click(object sender, EventArgs e)
        {
            List<IDeviceInfo>  deviceInfos= controller.RefreshDeviceList();
            if (deviceInfos != null)
            {
                comboBox1.Items.Clear();
                foreach (IDeviceInfo item in deviceInfos)
                {
                    comboBox1.Items.Add(item.DevTypeInfo.ToString() + ":" + item.ModelName + ":" + item.SerialNumber);
                }
            }
        }
        /// <summary>
        /// 初始化取相
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                if (controller.OpenCamer(comboBox1.SelectedIndex))
                {
                    label8.Text = "已初始化取相";
                    label8.ForeColor = System.Drawing.Color.Red;
                    button6.Enabled = false;
                    button7.Enabled = true;
                }
            }
        }
        /// <summary>
        /// 关闭取相按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
            private void button7_Click(object sender, EventArgs e)
        {
            if (controller.CloseCamer())
            {
                label8.Text = "未初始化取相";
                label8.ForeColor = System.Drawing.Color.Black;
                button6.Enabled = true; ;
                button7.Enabled = false;
                comboBox2.Items.Clear();
                comboBox3.Items.Clear();
                comboBox4.Items.Clear();
                comboBox5.Items.Clear();
                comboBox6.Items.Clear();
            }
        }
        /// <summary>
        /// 运行按钮 获取当前一帧的图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>+
       private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Run();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name+":" +ex.Message+"\r\n");
            }
        }
        /// <summary>
        /// 打开实时图像按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (realTimeCamerForm != null)
            {
                realTimeCamerForm.Show();
            }
        }
        /// <summary>
        /// 曝光改变时 
         /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            controller.SetExposure(float.Parse( numericUpDown1.Value.ToString()));
        }
        /// <summary>
        /// 亮度改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            controller.SetGain(float.Parse(numericUpDown2.Value.ToString()));
        }
        /// <summary>
        /// 对比度改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {

        }

        private void CamerForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            controller.Closed();
        }
        /// <summary>
        /// 帧率改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            controller.SetAcquisitionFrameRate(float.Parse( numericUpDown4.Value.ToString()));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1;
        }
        /// <summary>
        /// 修改相机配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            controller.SetCamerMode();
        }
        //属性值更改时
        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        //电平反转测试
        private void button10_Click(object sender, EventArgs e)
        {
            //电平信号
            controller.LevelReversal();
        }
        //恢复默认设置
        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                comboBox2.SelectedIndex = 0;
                comboBox3.SelectedIndex = 0;
                comboBox4.SelectedIndex=0;
                comboBox5.SelectedIndex=0;
                comboBox6.SelectedIndex=0;
            }
            catch { }

        }
    }
}
